package com.ruoyi.web.es.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.es.beans.*;
import com.ruoyi.web.es.impl.LogAllSystemMapper;
import com.ruoyi.web.es.impl.LogAllSystemSelectMapper;
import com.ruoyi.web.es.impl.UserMapper;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;


@Anonymous
@RestController
public class LogAllSystemController {

    @Autowired
    private Bdindex bdindex;

    @Autowired
    private LogAllSystemMapper logAllSystemMapper;

    @Autowired
    private LogAllSystemSelectMapper logAllSystemSelectMapper;

    @Autowired
    private ElasticsearchRestTemplate restTemplate;




    /***
     * 往es中添加值 logData
     */
    @PostMapping("/logDataAdd")
    public void logDataAdd(){
        //进行索引预处理
        bdindex.preindex();
        //时间格式处理
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<LogAllSystem> logAllSystemList = new ArrayList<>(10000);
        for (int i = 0; i < 10000; i++) {
            LogAllSystem logAllSystem = new LogAllSystem();
            logAllSystem.setId(UUID.randomUUID().toString());

            logAllSystem.setLogTime(formatter.format(new Date()));
            logAllSystem.setServiceName("苏二川"+i);
            logAllSystem.setJobNumber(new Random().nextInt(900000) + 100000 + "");
            logAllSystem.setUserName("苏彬"+i);
            logAllSystem.setIP(new Random().nextInt(256)+"");
            logAllSystem.setAction("下载");
            logAllSystem.setDescribe("用户下载");

            logAllSystem.setInTime(formatter.format(new Date()));
            logAllSystemList.add(logAllSystem);
        }
        logAllSystemMapper.saveAll(logAllSystemList);
    }

    /***
     * 根据服务器名字查找,模糊查找
     */
    @PostMapping("/logDataFindBySN")
    public List<LogAllSystem> logDataFindBySN(@RequestBody Map<String,String> map){
        //进行索引预处理
        bdindex.preindex();
        return logAllSystemMapper.findByServiceName(map.get("serviceName"));
    }

    /***
     * 根据服务器名字查找,模糊查找All
     */
    @PostMapping("/logDataFindBySNA")
    public List<LogAllSystemSelect> logDataFindBySNA(@RequestBody Map<String,String> map){
        ElasticSearchConfiguration.apiCallIndexNamePrefixA = "aqyy_log2024*";
        return logAllSystemSelectMapper.findByServiceName(map.get("serviceName"));
    }


    /***
     * 根据动作查找
     */
    @PostMapping("/logDataFindByAction")
    public List<LogAllSystem> logDataFindByAction(@RequestBody Map<String,String> map){
        //进行索引预处理
        bdindex.preindex();
        return logAllSystemMapper.findByServiceName(map.get("serviceName"));
    }

    /***
     *  usernames
     */
    @PostMapping("/logDataA")
    public List<LogAllSystem> findByServiceNamesQ(@RequestBody Map<String,List<String>> map){
        //进行索引预处理
        bdindex.preindex();
        return logAllSystemMapper.findByserviceNames(map.get("services"));
    }

    /***
     *  action、username进行查询
     */
    @PostMapping("/logDataB")
    public List<LogAllSystem> actionusername(@RequestBody Map<String,String> map){
        //进行索引预处理
        bdindex.preindex();
        return logAllSystemMapper.findByActionAndUserName(
                map.get("action"),map.get("userName")
        );
    }

    /***
     *  action、username进行查询
     */
    @PostMapping("/logDataC")
    public long countAll(@RequestBody Map<String,List<String>> map){
        ElasticSearchConfiguration.apiCallIndexNamePrefixA = "aqyy_log2024081*";
        // 构建 DSL 查询语句
        QueryBuilder queryBuilder = QueryBuilders.termQuery(map.get("service").get(0), map.get("service").get(1));
        // 将 QueryBuilder 转换为 Query
        Query query = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();
        return restTemplate.count(query, LogAllSystemSelect.class);
    }



    /***
     * 根据索引id删除值
     * @param map
     * @return
     */
    @PostMapping("/LogAllSystemDeleIndex")
    public boolean LogAllSystemDeleIndex(@RequestBody Map<String,String> map){
        String index = map.get("index");
        //传递索引名称
        ElasticSearchConfiguration.apiCallIndexNamePrefix = index;

        IndexOperations indexOperations = restTemplate.indexOps(LogAllSystem.class);

        return indexOperations.delete();
    }


}
